package com.qooco.watersystem;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import com.smartfoxserver.v2.SmartFoxServer;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class SchedulerTestExtension extends SFSExtension {

    private class TaskRunner implements Runnable
    {
        private int runningCycles = 0;
         
        public void run()
        {
            runningCycles++;   
            trace("Inside the running task. Cycle:  " + runningCycles);
             
            if (runningCycles >= 10)
            {
                trace("Time to stop the task!");
                taskHandle.cancel(true);
            }
        }
    }
     
    // Keeps a reference to the task execution
    ScheduledFuture<?> taskHandle;
	
    @Override
    public void init()
    {
        SmartFoxServer sfs = SmartFoxServer.getInstance();
        
        taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(new TaskRunner(), 0, 1, TimeUnit.SECONDS);
    }

}
